package com.atchangsha.system.controller;

import java.util.*;

import com.atchangsha.system.constant.CustomerConstants;
import com.atchangsha.system.dto.CusCouponDTO;
import com.atchangsha.system.dto.CustomerDTO;
import com.atchangsha.system.dto.DisCountDTO;
import com.atchangsha.system.dto.PhoneDto;
import com.atchangsha.system.feign.ThirdPartyFeignService;
import com.atchangsha.system.service.CouponNumberService;
import com.atchangsha.system.service.CouponService;
import com.atchangsha.system.service.CustomerCouponRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.atchangsha.system.entity.CustomerEntity;
import com.atchangsha.system.service.CustomerService;
import com.atchangsha.system.common.utils.PageUtils;
import com.atchangsha.system.common.utils.Result;

import javax.annotation.Resource;

/**
 * 顾客表
 *
 * @author 卓越班项目第二组
 * @date 2022-04-22 00:49:58
 */
@RestController
@RequestMapping("system/customer")
@CrossOrigin
@Slf4j
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @Resource
    private ThirdPartyFeignService thirdPartyFeignService;
    @Resource
    private CouponService couponService;
    @Resource
    private CouponNumberService couponNumberService;
    @Resource
    private CustomerCouponRelationService customerCouponRelationService;


    /**
     * 获取会员等级和折扣
     *
     * @param disCountDTO 将电话和原价封装成DTO
     * @return 会员等级和折扣
     */
    @PostMapping("/getMember")
    public Result getMember(@RequestBody DisCountDTO disCountDTO) {
        return customerService.getMember(disCountDTO);
    }

    /**
     * 分页查看会员列表
     *
     * @param param 分页信息
     * @return 分页数据
     */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> param) {
        PageUtils pageUtils = customerService.queryPage(param);
        return Result.ok().setResult(pageUtils);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{phone}")
    public Result info(@PathVariable("phone") String phone) {
        CustomerEntity customer = customerService.getOne(new LambdaQueryWrapper<CustomerEntity>().eq(CustomerEntity::getPhone, phone));
        return Result.ok().setResult(customer);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody CustomerEntity customer) {
        customerService.save(customer);
        return Result.ok();
    }


    /**
     * 注册会员
     */
    @PostMapping("/registerMember")
    public Result registerMember(@RequestBody PhoneDto phoneDto) {
        Boolean flag = thirdPartyFeignService.checkIsBoolean(phoneDto);
        if (!flag) {
            return Result.fail(CustomerConstants.CustomerEnum.FEIGN_CLIENT_FAIL);
        }
        CustomerEntity entity = customerService.getOne(new LambdaQueryWrapper<CustomerEntity>().eq(CustomerEntity::getPhone, phoneDto.getPhone()));
        if (entity != null) {
            return Result.fail(CustomerConstants.CustomerEnum.MEMBER_IS_EXISTING);
        }
        Boolean resFlag = customerService.registerMember(phoneDto);
        if (!resFlag) {
            return Result.fail(CustomerConstants.CustomerEnum.REGISTRY_MEMBER_FAIL);
        }
        return Result.ok(CustomerConstants.CustomerEnum.REGISTRY_MEMBER_SUCCESS);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody CustomerEntity customer) {
        boolean flag = customerService.updateById(customer);
        if (!flag) {
            return Result.fail(CustomerConstants.CustomerEnum.UPDATED_MEMBER_FAIL);
        }
        return Result.ok(CustomerConstants.CustomerEnum.UPDATED_MEMBER_SUCCESS);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long[] ids) {
        boolean flag = customerService.removeByIds(Arrays.asList(ids));
        if (!flag) {
            return Result.fail(CustomerConstants.CustomerEnum.DELETE_MEMBER_FAIL);
        }
        return Result.ok(CustomerConstants.CustomerEnum.DELETE_MEMBER_SUCCESS);
    }

    /**
     * 领取优惠卷
     * @param cusCouponDTO
     * @return
     */
    @GetMapping("/getCoupon")
    public Result getCoupon(@RequestBody CusCouponDTO cusCouponDTO) {
        return customerService.getCoupon(cusCouponDTO);
    }




}
